<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>编辑收货地址</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="format-detection" content="telephone=no" />
<jsp:include page="/jsp/inc_head.jsp"></jsp:include>
<style>
</style>
</head>

<body style="padding-bottom:50px;">
<form id="addressForm">
	<div class="am-g">
	<input type="hidden" id="addr_id" name="paddress.id" value="${address.id}">
	<input type="hidden" id="userId" name="paddress.userId" value="${address.userId}">
		<div class="am-u-sm-12 am-pad-none xzadrees_lie">
	    	<div class="am-u-sm-3 am-pad-none">
	        	联系人
	        </div>
	        <div class="am-u-sm-9 am-pad-none am-text-right">
	        	<input type="text" value="${address.name}" id="name" name="paddress.name"/>
	        </div>	
	    </div>
		<div class="am-u-sm-12 am-pad-none xzadrees_lie">
	    	<div class="am-u-sm-3 am-pad-none">
	        	联系电话
	        </div>
	        <div class="am-u-sm-9 am-pad-none am-text-right">
	        	<input type="tel" maxlength="12" value="${address.phone}" id="phone" name="paddress.phone"/>
	        </div>	
	    </div>
		<div class="am-u-sm-12 am-pad-none xzadrees_lie">
	    	<div class="am-u-sm-3 am-pad-none">
	        	收货地址
	        </div>
	        <div class="am-u-sm-9 am-pad-none am-text-right xzshdz">
	            <div class="am-u-sm-4 am-pad-none am-text-right">
	                <select id="provSelect" name="paddress.pro" onchange="changeProv(this.value)" style="width: 100%;">
	                    <c:forEach items="${areaProList}" var="type">
							<option value="${type.id }" <c:if test="${address.pro == type.id}">selected</c:if>>${type.text}</option>
						</c:forEach>
			        </select>
	            </div>
	            <div class="am-u-sm-4 am-pad-none am-text-right">
	                <select id="citySelect" name="paddress.city" onchange="changeCity(this.value)" style="width: 100%;">
	                   <c:forEach items="${areaCityList}" var="type">
	                   		<c:if test="${type.pid == address.pro}">
								<option value="${type.id }" <c:if test="${address.city == type.id}">selected</c:if>>${type.text}</option>
							</c:if>
						</c:forEach>
	                </select>
	            </div>
	            <div class="am-u-sm-4 am-pad-none am-text-right">
	                <select id="areaSelect" name="paddress.area" style="width: 100%;">
	                    <c:forEach items="${areaAreaList}" var="type">
	                  	  <c:if test="${type.pid == address.city}">
							<option value="${type.id }" <c:if test="${address.area == type.id}">selected</c:if>>${type.text}</option>
						  </c:if>
						</c:forEach>
	                </select>
	            </div>
	        </div>	
	    </div>
		<div class="am-u-sm-12 am-pad-none xzadrees_lie">
	    	<div class="am-u-sm-3 am-pad-none">
	        	详细地址
	        </div>
	        <div class="am-u-sm-9 am-pad-none am-text-right">
	        	<input type="text" value="${address.addDetail}" id="addDetail" name="paddress.addDetail"/>
	        </div>	
	    </div>   
		<div class="am-u-sm-12 am-pad-none xzadrees_lie">
	    	<div class="am-u-sm-6 am-pad-none">
	        	是否设为默认地址
	        </div>
	        <div class="am-u-sm-6 am-pad-none am-text-right am-text-right">
	        	<input type="hidden" id="isdefault" value="${address.isdefault}" name="paddress.isdefault"/>
	      		<c:if test="${address.isdefault == 0}">
		      		<input type="checkbox" id="swmr" class="addres_swmr" onchange="changeIsdefault(this)"/>
		            <label for="swmr" style="margin-bottom:0;"></label>
	      		</c:if>
	      		<c:if test="${address.isdefault == 1}">
	      			<input type="checkbox" id="swmr" class="addres_swmr" onchange="changeIsdefault(this)" checked/>
		            <label for="swmr" style="margin-bottom:0;"></label>
	      		</c:if>
	            <!-- <input type="checkbox" id="swmr" class="addres_swmr" checked />
	            <label for="swmr" style="margin-bottom:0;"></label> -->
	        </div>
	    </div>
	</div>
</form>
<div class="am-g btm_fixedsecond">
	<div class="am-u-sm-12 am-pad-none am-text-center">
    	<div class="am-u-sm-6 am-pad-none am-text-center btm_cancel">
        	<div onclick="toDeleteThis()">删除</div>
        </div>
        <div class="am-us-m-6 am-pad-none am-text-center btm_save">
        	<div onClick="submitUpdateForm()">保存</div>
        </div>
    </div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn" onClick="Sure()">确定</span>
		</div>
	</div>
</div>
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert2">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title2">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message2"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn">确定</span>
		</div>
	</div>
</div>
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert3">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title3">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message3"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn" onClick="Sure3()">确定</span>
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
var ajaxpath="${pageContext.request.contextPath}";
var cityList = eval('${cityJson}');
var areaList = eval('${areajson}');
//手机格式验证
var reg =new RegExp("[\\u4E00-\\u9FFF]+","g");
var reg2= /^[A-Za-z]+$/;

//更改默认设置
function changeIsdefault(obj)
{
	if($(obj).prop("checked")){
		$("#isdefault").val(1);
	}else{
		$("#isdefault").val(0);
	}
}

//选择省
function changeProv(id)
{
	var cityOne = "-1";
	var content = "";
	if(id!=-1){
		for (var i = 0; i < cityList.length; i++) {
			if(cityList[i].pid == id)
			{
				content +='<option value="'+cityList[i].id+'">'
					+ cityList[i].text + '</option>';
				if(cityOne == "-1")
				{
					cityOne = cityList[i].id;
				}
			}
		}
		$("#citySelect").html(content);
	}else{
		$("#citySelect").html('<option value="-1">选择城市</option>');
	}
	changeCity(cityOne);
}


//选择市
function changeCity(id)
{
	var content = "";
	if(id!=-1){
		for (var i = 0; i < areaList.length; i++) {
			if(areaList[i].pid == id)
			{
				content +='<option value="'+areaList[i].id+'">'
					+ areaList[i].text + '</option>';
			}
		}
		$("#areaSelect").html(content);
	}else{
		$("#areaSelect").html('<option value="-1">选择区域</option>');
	}
}
//删除该地址
function toDeleteThis(){
	$("#alert_title").html("提示");
	$("#alert_message").html("你确定要删除该地址！");
	$("#my-alert").modal('open');
}
function Sure(){
	var id=$("#addr_id").val();
	$.ajax({
		url : ajaxpath+'/address/deleteAddress.do?id='+id,
		type : 'post',
		async : false,
		success : function(data)
		{
			if(data.success){
				var json ='${json}';
				if(json!=null&&json!=undefined&&json!='')
				{location.href=ajaxpath+"/client_toAddress2.do?json="+json;}
				else
				{location.href=ajaxpath+"/client_toAddress.do";}
				//window.location.href=ajaxpath+"/client_toAddress.do";
			}else{
				$("#alert_message2").html(data.msg);
				$("#my-alert2").modal('open');
			}
			
		}
	});
}
function submitUpdateForm(){
	var flag = true;
	var msg = "";
	if(flag)if($("#name").val() == "")	{msg = "请填写姓名！";flag = false;}
	if(flag)if($("#phone").val() == "")	{msg = "请填写电话！";flag = false;}
	if(flag)if(reg.test($("#phone").val())||reg2.test($("#phone").val())){msg = "手机号码格式错误！";flag = false;}
	if(flag)if($("#citySelect").val() == ""){msg = "请选择城市！";flag = false;}
	if(flag)if($("#addDetail").val() == "")	{msg = "请填写详细地址！";flag = false;}
	if(flag){
		var paddress={};//注册对象
		var url_submit = ajaxpath+"/address/updateAddress.do";
		paddress.id=$("#addr_id").val();
		paddress.userId=$("#userId").val();
		paddress.name=$("#name").val();
		paddress.phone=$("#phone").val();
		paddress.pro=$("#provSelect").val();
		paddress.city=$("#citySelect").val();
		paddress.area=$("#areaSelect").val();
		paddress.addDetail=$("#addDetail").val();
		paddress.isdefault=$("#isdefault").val();
		$.post(url_submit, paddress, function(data) {
			if(data.success){
				var json ='${json}';
				if(json!=null&&json!=undefined&&json!='')
				{location.href=ajaxpath+"/client_toAddress2.do?json="+json;}
				else
				{location.href=ajaxpath+"/client_toAddress.do";}
			}else{
				$("#alert_message3").html(data.msg);
				$("#my-alert3").modal('open');
			}
		});
	}else{
		$("#alert_message2").html(msg);
		$("#my-alert2").modal('open'); 
	}
}

function Sure3(){
	var json ='${json}';
	if(json!=null&&json!=undefined&&json!='')
	{location.href=ajaxpath+"/client_toAddress2.do?json="+json;}
	else
	{location.href=ajaxpath+"/client_toAddress.do";}
}
</script>
</html>
